<?php

if ( !defined('IN_LC') )
{
	die('Hacking attempt1');
}



session_start();

//set_magic_quotes_runtime(0); // Disable magic_quotes_runtime



global $db, $config, $dss_seeded, $page_title, $properties, $lang, $skin_dir, $www_forums, $www_path, $forum_avatar_dir, $style;
$board_config = array();
$dss_seeded = false;

include_once("config.php");
include_once("includes/functions.php");


if(!defined('MAINTENANCE') && $properties['closed'] == true)
{
	$page_title .= ' (Maintenance Mode)';
	
	if(! isset($_SESSION["valid_admin"]) || $_SESSION["valid_admin"] < 3)
		Header("Location: maintenance.php");
	
}

// Protect against GLOBALS tricks
if (isset($_POST['GLOBALS']) || isset($_FILES['GLOBALS']) || isset($_GET['GLOBALS']) || isset($_COOKIE['GLOBALS']))
{
	die("Hacking attempt2");
}

// Protect against _SESSION tricks
if (isset($_SESSION) && !is_array($_SESSION))
{
	die("Hacking attempt3");
}


if (@ini_get('register_globals') == '1' || strtolower(@ini_get('register_globals')) == 'on')
{
	// PHP4+ path
	$not_unset = array('_GET', '_POST', '_COOKIE', '_SERVER', '_SESSION', '_ENV', '_FILES', 'phpEx', 'phpbb_root_path');

	// Not only will array_merge give a warning if a parameter
	// is not an array, it will actually fail. So we check if
	// _SESSION has been initialised.
	if (!isset($_SESSION) || !is_array($_SESSION))
	{
		$_SESSION = array();
	}

	// Merge all into one extremely huge array; unset
	// this later
	$input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_SESSION, $_ENV, $_FILES);

	unset($input['input']);
	unset($input['not_unset']);

	while (list($var,) = @each($input))
	{
		if (in_array($var, $not_unset))
		{
			die('Hacking attempt!');
		}
		unset($$var);
	}

	unset($input);
}


//
// addslashes to vars if magic_quotes_gpc is off
// this is a security precaution to prevent someone
// trying to break out of a SQL statement.
//
if( !get_magic_quotes_gpc() )
{
	if( is_array($_GET) )
	{
		while( list($k, $v) = each($_GET) )
		{
			if( is_array($_GET[$k]) )
			{
				while( list($k2, $v2) = each($_GET[$k]) )
				{
					$_GET[$k][$k2] = addslashes($v2);
				}
				@reset($_GET[$k]);
			}
			else
			{
				$_GET[$k] = addslashes($v);
			}
		}
		@reset($_GET);
	}

	if( is_array($_POST) )
	{
		while( list($k, $v) = each($_POST) )
		{
			if( is_array($_POST[$k]) )
			{
				while( list($k2, $v2) = each($_POST[$k]) )
				{
					$_POST[$k][$k2] = addslashes($v2);
				}
				@reset($_POST[$k]);
			}
			else
			{
				$_POST[$k] = addslashes($v);
			}
		}
		@reset($_POST);
	}

	if( is_array($_COOKIE) )
	{
		while( list($k, $v) = each($_COOKIE) )
		{
			if( is_array($_COOKIE[$k]) )
			{
				while( list($k2, $v2) = each($_COOKIE[$k]) )
				{
					$_COOKIE[$k][$k2] = addslashes($v2);
				}
				@reset($_COOKIE[$k]);
			}
			else
			{
				$_COOKIE[$k] = addslashes($v);
			}
		}
		@reset($_COOKIE);
	}
}


include_once($language);
include_once("includes/constants.php");
include_once("includes/db.php");

define('IN_PHPBB', true);
include_once($absolute_forums . "includes/functions.php");

// We do not need this any longer, unset for safety purposes
unset($dbpasswd);


//sanatise the values
if(isset($_COOKIE))
{
	foreach($_COOKIE as $key => $value)
	{
		$_COOKIE[$key] = htmlspecialchars(mysql_real_escape_string(strip_tags($value)));
	}
}

//sanatise the values
if(isset($_POST))
{
	foreach($_POST as $key => $value)
	{
		if(is_array($value))
		{
			foreach($value as $subKey => $subValue)
			{
				$_POST[$key][$subKey] = htmlspecialchars(mysql_real_escape_string(strip_tags($subValue)));
			}
		}
		else
		{
			$_POST[$key] = htmlspecialchars(mysql_real_escape_string(strip_tags($value)));
		}
	}
}


//sanatise the values
/*if(isset($_SERVER))
{
	foreach($_SERVER as $key => $value)
	{
		$_SERVER[$key] = htmlspecialchars(mysql_real_escape_string(strip_tags($value)));
	}
}*/

//sanatise the values
if(isset($_GET))
{
	foreach($_GET as $key => $value)
	{
		$_GET[$key] = htmlspecialchars(mysql_real_escape_string(strip_tags($value)));
	}
}

//sanatise the values
if(isset($_ENV))
{
	foreach($_ENV as $key => $value)
	{
		$_ENV[$key] = htmlspecialchars(mysql_real_escape_string(strip_tags($value)));
	}
}

//
// Obtain and encode users IP
//
// I'm removing HTTP_X_FORWARDED_FOR ... this may well cause other problems such as
// private range IP's appearing instead of the guilty routable IP, tough, don't
// even bother complaining ... go scream and shout at the idiots out there who feel
// "clever" is doing harm rather than good ... karma is a great thing ... :)
//
$client_ip = ( !empty($_SERVER['REMOTE_ADDR']) ) ? $_SERVER['REMOTE_ADDR'] : ( ( !empty($_ENV['REMOTE_ADDR']) ) ? $_ENV['REMOTE_ADDR'] : getenv('REMOTE_ADDR') );
$user_ip = encode_ip($client_ip);


//
// Setup forum wide options, if this fails
// then we output a CRITICAL_ERROR since
// basic forum information is not available
//
$config = array();
$sql = "SELECT *
	FROM " . CONFIG_TABLE;
if( !($result = $db->sql_query($sql)) )
{
	die("CRITICAL_ERROR: Could not query config information");
}

while ( $row = $db->sql_fetchrow($result) )
{
	$config[$row['config_name']] = $row['config_value'];
}

// Check for a cookie
if(isset($_COOKIE['sessionID']) && isset($_SESSION['valid_id'])) 
{
    // Try to find a match in the database
    $sessionID = $_COOKIE['sessionID'];
    $validID = $_SESSION['valid_id'];
    $sQuery = "Select user_id From ". USERS_TABLE . " Where lc_sessionid = '$sessionID' AND user_id = $validID";
    $result = $db->sql_query($sQuery);
    if(!$row = $db->sql_fetchrow($result)) {        
        // User is not logged in
        session_unset();
        session_destroy();
    }
}
else if(isset($_COOKIE['sessionID']) && isset($_COOKIE['userID'])) 
{
	// Create query
	$q = "SELECT *, pf_drivername AS user_drivername ". 
		" FROM " . USERS_TABLE . " u, " . LCUSERS_TABLE . " lu, " . DRIVERNAME_TABLE . " d 
		  WHERE lu.u_id = u.user_id AND u.user_id = d.user_id AND u.user_id=".$_COOKIE["userID"]." AND u.lc_sessionid='".$_COOKIE["sessionID"]."' LIMIT 1";
	// Run query
	
	$res = $db->sql_query($q);
	if ( $row = $db->sql_fetchrow($res)  )
	{
		// Login good, create session variables
		$_SESSION["valid_id"] = $row['user_id'];
		$_SESSION["valid_user"] = $row["username"];
		$_SESSION["valid_driver"] = $row["user_drivername"];
		$_SESSION["valid_time"] = time();
		$_SESSION["valid_admin"] = $row["u_admin"];
	}
}
else
{
    session_unset();
    session_destroy();
}




?>